package org.particlethink.model;


import org.particlethink.entry.PlanTyp;
import org.particlethink.handler.PlanTypHandler;
import tk.mybatis.mapper.annotation.ColumnType;
import tk.mybatis.mapper.annotation.KeySql;

import javax.persistence.Id;
import javax.persistence.Table;
import java.math.BigDecimal;
import java.util.Date;

@Table(name = "plan_materials")
public class PlanMaterial {//批次关联的物料
    @Id
    @KeySql(useGeneratedKeys = true)
    private Integer id;
    private Date createdAt;
    private Date updatedAt;
    private Integer materialId;//物料id


    private Integer planId; //planId

    @ColumnType(typeHandler = PlanTypHandler.class)
    private PlanTyp typ;//计划类型

    private Integer expectNum;//计划数量
    private Integer actualNum;//已规划数量
    private Integer completeNum;//已入库数量

    private Integer supplierId;//供货商id
    private String purpose;//使用部位
    private String remark;//备注
    private Integer orderId;//订单id 用来关联order

    public PlanMaterial(Integer materialId, Integer planId, PlanTyp typ, Integer expectNum, String remark) {
        this.materialId = materialId;
        this.planId = planId;
        this.typ = typ;
        this.expectNum = expectNum;
        this.remark = remark;
    }

    public PlanMaterial() {

    }

    public PlanMaterial(Integer materialId, Integer planId, PlanTyp typ, Integer expectNum, Integer supplierId, String purpose, String remark,Integer actualNum) {
        this.materialId = materialId;
        this.planId = planId;
        this.typ = typ;
        this.expectNum = expectNum;
        this.remark = remark;
        this.actualNum = actualNum;
        this.supplierId = supplierId;
        this.purpose = purpose;
    }


    public Integer getPlanId() {
        return planId;
    }

    public void setPlanId(Integer planId) {
        this.planId = planId;
    }

    public String getPurpose() {
        return purpose;
    }

    public void setPurpose(String purpose) {
        this.purpose = purpose;
    }


    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getId() {
        return id;
    }

    public void setCreatedAt(Date createdAt) {
        this.createdAt = createdAt;
    }

    public Date getCreatedAt() {
        return createdAt;
    }

    public void setUpdatedAt(Date updatedAt) {
        this.updatedAt = updatedAt;
    }

    public Date getUpdatedAt() {
        return updatedAt;
    }

    public void setMaterialId(Integer materialId) {
        this.materialId = materialId;
    }

    public Integer getMaterialId() {
        return materialId;
    }

    public void setTyp(PlanTyp typ) {
        this.typ = typ;
    }

    public PlanTyp getTyp() {
        return typ;
    }


    public void setExpectNum(Integer expectNum) {
        this.expectNum = expectNum;
    }

    public Integer getExpectNum() {
        return expectNum == null ? 0 : expectNum;
    }

    public void setActualNum(Integer actualNum) {
        this.actualNum = actualNum;
    }

    public Integer getActualNum() {

        return actualNum == null ? 0 : actualNum;
    }

    public void setCompleteNum(Integer completeNum) {
        this.completeNum = completeNum == null ? 0 : completeNum;
    }

    public Integer getCompleteNum() {
        return completeNum == null ? 0 : completeNum;
    }

    public void setSupplierId(Integer supplierId) {
        this.supplierId = supplierId == null ? 0 : supplierId;
    }

    public Integer getSupplierId() {
        return supplierId;
    }

    public Integer getOrderId() {
        return orderId;
    }

    public void setOrderId(Integer orderId) {
        this.orderId = orderId;
    }


    //规划率
    public Integer planningRate() {
        BigDecimal result = new BigDecimal(actualNum).multiply(new BigDecimal(100)).divide(new BigDecimal(expectNum), 2, BigDecimal.ROUND_HALF_UP);
        return result.toBigInteger().intValue();
    }

    //    入库率
    public Integer inventoryRate() {
        BigDecimal result = new BigDecimal(completeNum).multiply(new BigDecimal(100)).divide(new BigDecimal(expectNum), 2, BigDecimal.ROUND_HALF_UP);
        return result.toBigInteger().intValue();
    }


    //偏差预警
    public String planDeviation(Integer rate) {
        if (rate <= 70) {
            return "正常";
        }
        if (rate <= 100) {
            return "提醒";
        }
        if (rate <= 130) {
            return "预警";
        }

        return "告警";
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }
}
//todo 收货更新 actualNum completeNum
